<?php
/**
 * 优惠券模型
 * @author lee
 */
namespace app\common\model;

class Coupon extends SoftDel
{
    public $tableName = '优惠券';                   // 数据表名称
    public $logId     = ['id', 'coupon_name'];     // 日志操作相关标识
    public $softId    = 1;                        // 软删除标识

    public function fields()
    {
        $field ='id,coupon_name,description,offset_price,limit_price,scope,valid_time,register_time,pbegin,pend';
        return $field;
    }

    /**
     * 变动时间获取器
     * @param  string $vale 字段值
     */
    public function getPendTxtAttr($value,$data)
    {
        return date('Y-m-d H:i:s', $data['pend']);
    }

    /**
     * 变动时间获取器
     * @param  string $vale 字段值
     */
    public function getPbeginTxtAttr($value,$data)
    {
        return date('Y-m-d', $data['pbegin']);
    }

    /**
     * 获取我的领取id
     */
    public function getMeCouponIdAttr($key,$val){
        if(!$val['userid']){
            return 0;
        }

        $where=[
            'userid'=>$val['userid'],
            'coupon_id'=>$val['id'],
        ];
        return CouponMe::where($where)->value('id');
    }

	/**
	 * 优惠券类型获取器
	 * @param  string $vale 字段值
	 */
	public function getTypeTxtAttr($value,$data)
	{
		$array = [0=>'满减',1=>'抵用'];
		return $array[$data['coupon_type']];
	}
    public function getClassTxtAttr($value,$data)
    {
        return CouponClass::where("id",$data['class_id'])->value('class_name');

    }
    public function getImageUriAttr($value, $data)
    {
        return $data['share_img'] ? attach_uri($data['share_img']) : '';
    }
    public function getStatusTxtAttr($value,$data){
	    $status = [0=>"正常",1=>"禁用"];
	    return $status[$data['status']];
    }
}
